home *** CD-ROM | disk | FTP | other *** search
/ Scene Storm / Scene Storm - Volume 1.iso / coding / c / clipboard / cbio.c next >
C/C++ Source or Header  |  1995-11-05  |  5KB  |  200 lines

  1. /*********************************************************************/
  2. /*                                                                   */
  3. /*  Program name:  cbio                                              */
  4. /*                                                                   */
  5. /*  Purpose:  To provide standard clipboard device interface routines*/
  6. /*            such as Open, Post, Read, Write, etc.                  */
  7. /*  (C) 1986 Commodore-Amiga, Inc.                                   */
  8. /* Permission given to use and distribute this code as long as this  */
  9. /* notice remains intact.                                            */
  10. /*********************************************************************/
  11. #include "exec/types.h"
  12. #include "exec/ports.h"
  13. #include "exec/io.h"
  14. #include "devices/clipboard.h"
  15.  
  16.  
  17. struct IOClipReq clipboardIO = 0;
  18. struct MsgPort clipboardMsgPort = 0;
  19. struct MsgPort satisfyMsgPort = 0;
  20.  
  21. int CBOpen(unit)
  22. int unit;
  23. {
  24.     int error;
  25.  
  26.     /* open the clipboard device */
  27.     if ((error = OpenDevice("clipboard.device", unit, &clipboardIO, 0)) != 0)
  28.         return(error);
  29.  
  30.     /* Set up the message port in the I/O request */
  31.     clipboardMsgPort.mp_Node.ln_Type = NT_MSGPORT;
  32.     clipboardMsgPort.mp_Flags = 0;
  33.     clipboardMsgPort.mp_SigBit = AllocSignal(-1);
  34.     clipboardMsgPort.mp_SigTask = (struct Task *) FindTask((char *) NULL);
  35.     AddPort(&clipboardMsgPort);
  36.     clipboardIO.io_Message.mn_ReplyPort = &clipboardMsgPort;
  37.  
  38.     satisfyMsgPort.mp_Node.ln_Type = NT_MSGPORT;
  39.     satisfyMsgPort.mp_Flags = 0;
  40.     satisfyMsgPort.mp_SigBit = AllocSignal(-1);
  41.     satisfyMsgPort.mp_SigTask = (struct Task *) FindTask((char *) NULL);
  42.     AddPort(&satisfyMsgPort);
  43.     return(0);
  44. }
  45.  
  46. CBClose()
  47. {
  48.     RemPort(&satisfyMsgPort);
  49.     RemPort(&clipboardMsgPort);
  50.     CloseDevice(&clipboardIO);
  51. }
  52.  
  53. CBSatisfyPost(string,length)
  54. char *string;
  55. int length;
  56. {
  57.     clipboardIO.io_Offset = 0;
  58.     writeLong("FORM");
  59.     length += 12;
  60.     writeLong(&length);
  61.     writeLong("FTXT");
  62.     writeLong("CHRS");
  63.     length -= 12;
  64.     writeLong(&length);
  65.  
  66.     clipboardIO.io_Command = CMD_WRITE;
  67.     clipboardIO.io_Data = string;
  68.     clipboardIO.io_Length = length;
  69.     DoIO(&clipboardIO);
  70.  
  71.     clipboardIO.io_Command = CMD_UPDATE;
  72.     return(DoIO(&clipboardIO));
  73. }
  74.  
  75. writeLong(ldata)
  76. LONG *ldata;
  77. {
  78.  
  79. int status;
  80.     clipboardIO.io_Command = CMD_WRITE;
  81.     clipboardIO.io_Data = ldata;
  82.     clipboardIO.io_Length = 4;
  83.     status=(DoIO(&clipboardIO));
  84. }
  85.  
  86. CBCutS(string,length)
  87. char *string;
  88. int length;
  89. {
  90.     clipboardIO.io_ClipID = 0;
  91.     return(CBSatisfyPost(string,length));
  92. }
  93.  
  94. int
  95. CBPasteS(string)
  96. char *string;
  97. {
  98.     int length=0,status=0;
  99.  
  100.     clipboardIO.io_Command = CMD_READ; /* get the FORM */
  101.     clipboardIO.io_Data = string;
  102.     clipboardIO.io_Length = 4;
  103.     clipboardIO.io_Offset = 0;
  104.     clipboardIO.io_ClipID = 0;
  105.     status -= DoIO(&clipboardIO);
  106.     string[4]='\0';
  107.  
  108.     if(!strcmp(string,"FORM")) { /* iff form */
  109.         clipboardIO.io_Command = CMD_READ; /* get the total length */
  110.         clipboardIO.io_Data = &length;
  111.         clipboardIO.io_Length = 4;
  112.         status -=DoIO(&clipboardIO);
  113.  
  114.         clipboardIO.io_Command = CMD_READ; /* read the chunk and body */
  115.         clipboardIO.io_Data = string;
  116.         clipboardIO.io_Length = 8;
  117.         status -=DoIO(&clipboardIO);
  118.         string[8]='\0';
  119.  
  120.         if(!strcmp(string,"FTXTCHRS")) {
  121.         clipboardIO.io_Command = CMD_READ; /* get the length of the data */
  122.         clipboardIO.io_Data = &length;
  123.         clipboardIO.io_Length = 4;
  124.         status -=DoIO(&clipboardIO);
  125.  
  126.         clipboardIO.io_Command = CMD_READ;
  127.         clipboardIO.io_Data = string;
  128.         clipboardIO.io_Length = length;
  129.         status -=DoIO(&clipboardIO);
  130.         }
  131.     }
  132.     /* force end of file to terminate read */
  133.         clipboardIO.io_Command = CMD_READ;
  134.         clipboardIO.io_Length = 1;
  135.         clipboardIO.io_Data = 0;
  136.         status -= DoIO(&clipboardIO);
  137.  
  138.         if(!status)return(length);
  139.         else return(-1);
  140. }
  141.  
  142. int
  143. CBCurrentReadID()
  144. {
  145.     clipboardIO.io_Command = CBD_CURRENTREADID;
  146.     DoIO(&clipboardIO);
  147.     return(clipboardIO.io_ClipID);
  148. }
  149.  
  150. int
  151. CBCurrentWriteID()
  152. {
  153.     clipboardIO.io_Command = CBD_CURRENTWRITEID;
  154.     DoIO(&clipboardIO);
  155.     return(clipboardIO.io_ClipID);
  156. }
  157.  
  158. BOOL
  159. CBCheckSatisfy(idVar)
  160. int *idVar;
  161. {
  162.     struct SatisfyMsg *sm;
  163.  
  164.     if (*idVar == 0)
  165.         return(TRUE);
  166.     if (*idVar < CBCurrentWriteID()) {
  167.         *idVar = 0;
  168.         return(TRUE);
  169.     }
  170.     if (sm = (struct SatisfyMsg *) GetMsg(&satisfyMsgPort)) {
  171.         if (*idVar == sm->sm_ClipID)
  172.             return(TRUE);
  173.     }
  174.     return(FALSE);
  175. }
  176.  
  177. CBCut(stream, length)
  178. char *stream;
  179. int length;
  180. {
  181.     clipboardIO.io_Command = CMD_WRITE;
  182.     clipboardIO.io_Data = stream;
  183.     clipboardIO.io_Length = length;
  184.     clipboardIO.io_Offset = 0;
  185.     clipboardIO.io_ClipID = 0;
  186.     DoIO(&clipboardIO);
  187.     clipboardIO.io_Command = CMD_UPDATE;
  188.     DoIO(&clipboardIO);
  189. }
  190.  
  191. int
  192. CBPost()
  193. {
  194.     clipboardIO.io_Command = CBD_POST;
  195.     clipboardIO.io_Data = &satisfyMsgPort;
  196.     clipboardIO.io_ClipID = 0;
  197.     DoIO(&clipboardIO);
  198.     return(clipboardIO.io_ClipID);
  199. }
  200.